package com.so206.controller.pay.process;

import com.so206.po.SystemUser;
import com.so206.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 用户资金操作
 */
@Component
public class UserBalnesChange {

    private static final Logger logger = LoggerFactory.getLogger(UserBalnesChange.class);

    //多线程锁对象,常量
    private static final Object locked = new Object();

    @Autowired
    private UserService userService;

    /**
     * 获取用户余额,如果正在其他操作,就先等着
     */
    public BigDecimal getUserbalnes(Long uid) {
        synchronized (locked) {
            logger.info("线程:" + Thread.currentThread().getName() + " 获取用户余额:" + uid);
            SystemUser user = userService.findUserById(uid);
            if (user != null) {
                return user.getBalnes();
            }
            return null;
        }
    }

    /**
     * 设置用户余额,如果正在其他操作,就先等着
     */
    public boolean setUserBlanes(Long uid, BigDecimal money) {
        synchronized (locked) {
            logger.info("线程:" + Thread.currentThread().getName() + " 修改用户:" + uid + " 余额为:" + money);
            SystemUser user = userService.findUserById(uid);
            if (user != null) {
                user.setBalnes(money);
                userService.updateUser(user);
                return true;
            }
        }
        return false;
    }

    /**
     * 给用户加钱,如果正在减钱就先等等
     */
    public boolean addUserBalnes(Long uid, BigDecimal add) {
        synchronized (locked) {
            logger.info("线程:" + Thread.currentThread().getName() + " 给用户加钱:" + add);
            SystemUser user = userService.findUserById(uid);
            if (user != null) {
                BigDecimal balnes = user.getBalnes();
                BigDecimal scale = balnes.add(add).setScale(2, BigDecimal.ROUND_HALF_UP);
                user.setBalnes(scale);
                userService.updateUser(user);
                return true;
            }
            return false;
        }
    }

    /**
     * 给用户减钱,如果正在加钱就先等等
     */
    public boolean subUserBalnes(Long uid, BigDecimal sub) {
        synchronized (locked) {
            logger.info("线程:" + Thread.currentThread().getName() + " 给用户减钱:" + sub);
            SystemUser user = userService.findUserById(uid);
            if (user != null) {
                BigDecimal balnes = user.getBalnes();
                BigDecimal scale = balnes.subtract(sub).setScale(2, BigDecimal.ROUND_HALF_UP);
                user.setBalnes(scale);
                userService.updateUser(user);
                return true;
            }
            return false;
        }
    }

    /**
     * 转账操作,如果正在加钱就先等等
     */
    public boolean transafer(Long src_pid, Long dst_pid, BigDecimal money) {
        synchronized (locked) {
            SystemUser src_user = userService.findUserById(src_pid);
            if (src_user == null) {
                return false;
            }
            SystemUser dst_user = userService.findUserById(dst_pid);
            if (dst_user == null) {
                return false;
            }
            if (money.compareTo(new BigDecimal(0.00)) <= 0) {
                return false;
            }
            if (src_user.getBalnes().compareTo(new BigDecimal(0.00)) <= 0) {
                return false;
            }
            if (src_user.getBalnes().compareTo(money) < 0) {
                return false;
            }
            src_user.setBalnes(src_user.getBalnes().subtract(money).setScale(2, BigDecimal.ROUND_HALF_UP));
            dst_user.setBalnes(dst_user.getBalnes().add(money).setScale(2, BigDecimal.ROUND_HALF_UP));
            userService.updateUser(src_user);
            userService.updateUser(dst_user);
            return true;
        }
    }

}
